﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Function_Analyzer
{
    class Util
    {
        public static string ReplaceVar(string exp, char from, string to)
        {
            exp = exp.ToLower();
            int i = 0;
            while (i < exp.Length)
            {
                bool factor1 = false;
                bool factor2 = false;
                if (exp[i] == from)
                {
                    if (i > 0)
                    {
                        if (!Char.IsLetter(exp[i - 1])) factor1 = true;
                    }
                    else
                    {
                        factor1 = true;
                    }
                    if (i < exp.Length - 1)
                    {
                        if (!Char.IsLetter(exp[i + 1])) factor2 = true;
                    }
                    else
                    {
                        factor2 = true;
                    }
                    if (factor1 && factor2)
                    {
                        exp = Util.ReplaceOneTime(exp, i, 1, to);
                        i += to.Length;
                        continue;
                    }
                }
                i++;
            }
            return exp;
        }

        public static string ReplaceOneTime(string exp, int from, int length, string replacement)
        {
            return exp.Substring(0, from) + replacement + exp.Substring(from + length);
        }

        public static bool IsNumericStr(string exp)
        {
            double myInt;
            return double.TryParse(exp, out myInt);
        }

        public static bool IsFunc(string st)
        {
            if (st == "sin" || st == "cos" || st == "tan" || st == "sec" || st == "csc" || st == "cot" || st == "asin" || st == "acos" || st == "atan" || st == "acsc" || st == "asec" || st == "acot" || st == "sinh" || st == "cosh" || st == "tanh" || st == "sech" || st == "csch" || st == "coth" || st == "asinh" || st == "acosh" || st == "atanh" || st == "acsch" || st == "asech" || st == "acoth" || st == "log" || st == "lg" || st == "ln" || st == "abs" || st == "sgn" || st == "int" || st == "sqr" || st == "sqrt" || st == "exp") return true;
            return false;
        }

        public static bool IsOper(string st)
        {
            if (st == "+" || st == "-" || st == "*" || st == "/" || st == "^") return true;
            return false;
        }

        public static double Larger(double x1, double x2){
            return (x1>x2)?x1:x2;
        }

        public static double Smaller(double x1, double x2){
            return (x1<x2)?x1:x2;
        }


    }
}
